<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>mvc延伸：mvp、mvvm | haijd</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="Asp.Net MVC" />
  
  
  
  
  <meta name="description" content="MVC延伸：MVP、MVVM">
<meta name="keywords" content="Asp.Net MVC">
<meta property="og:type" content="article">
<meta property="og:title" content="MVC延伸：MVP、MVVM">
<meta property="og:url" content="http://www.hais2.com/2015/09/19/Extends-MVC-MVP-and-MVVM/index.html">
<meta property="og:site_name" content="haijd">
<meta property="og:description" content="MVC延伸：MVP、MVVM">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://ww2.sinaimg.cn/mw690/c55a7aeejw1f1d510o2nfj20dt08ht8y.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/mw690/c55a7aeejw1f17gi6rfa9j20e308yaac.jpg">
<meta property="og:image" content="http://ww2.sinaimg.cn/mw690/c55a7aeejw1f17gunrixcj20dp090t8z.jpg">
<meta property="og:updated_time" content="2017-08-02T15:22:33.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="MVC延伸：MVP、MVVM">
<meta name="twitter:description" content="MVC延伸：MVP、MVVM">
<meta name="twitter:image" content="http://ww2.sinaimg.cn/mw690/c55a7aeejw1f1d510o2nfj20dt08ht8y.jpg">
  
    <link rel="alternate" href="/atom.xml" title="haijd" type="application/atom+xml">
  
  <link rel="icon" href="/css/images/favicon.ico">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/9749f0/00000000000000000001008f/27/l?subset_id=2&fvd=n5) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/90cf9f/000000000000000000010091/27/l?subset_id=2&fvd=n7) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/8a5494/000000000000000000013365/27/l?subset_id=2&fvd=n4) format("woff2");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/d337d8/000000000000000000010095/27/l?subset_id=2&fvd=i4) format("woff2");font-weight:400;font-style:italic;}</style>
    
  <link rel="stylesheet" id="athemes-headings-fonts-css" href="//fonts.googleapis.com/css?family=Yanone+Kaffeesatz%3A200%2C300%2C400%2C700&amp;ver=4.6.1" type="text/css" media="all">
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css" >
  <link rel="stylesheet" href="/css/hiero.css" >
  <link rel="stylesheet" href="/css/glyphs.css" >
  
    <link rel="stylesheet" href="/css/vdonate.css" >
  

</head>

<script>
var themeMenus = {};

  themeMenus["/"] = "Home"; 

  themeMenus["/archives"] = "Archives"; 

  themeMenus["/categories"] = "Categories"; 

  themeMenus["/tags"] = "Tags"; 

  themeMenus["/about"] = "About"; 

</script>


  <body data-spy="scroll" data-target="#toc" data-offset="50">


  <header id="allheader" class="site-header" role="banner">
  <div class="clearfix container">
      <div class="site-branding">

          <h1 class="site-title">
            
              <a href="/" title="haijd" rel="home"> haijd </a>
            
          </h1>

          
            <div class="site-description">Stay Hungry,Stay Foolish</div>
          
            
          <nav id="main-navigation" class="main-navigation" role="navigation">
            <a class="nav-open">Menu</a>
            <a class="nav-close">Close</a>
            <div class="clearfix sf-menu">

              <ul id="main-nav" class="nmenu sf-js-enabled">
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/">Home</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/archives">Archives</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/categories">Categories</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/tags">Tags</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/about">About</a> </li>
                    
              </ul>
            </div>
          </nav>


      </div>
  </div>
</header>




  <div id="container">
    <div id="wrap">
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-Extends-MVC-MVP-and-MVVM" style="width: 66%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
  <div id="articleInner" class="clearfix post-1016 post type-post status-publish format-standard has-post-thumbnail hentry category-template-2 category-uncategorized tag-codex tag-edge-case tag-featured-image tag-image tag-template">
    
    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      MVC延伸：MVP、MVVM
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	Posted on <a href="/2015/09/19/Extends-MVC-MVP-and-MVVM/" class="article-date">
	  <time datetime="2015-09-18T17:16:16.000Z" itemprop="datePublished">September 19, 2015</time>
	</a>

      
	<span id="busuanzi_container_page_pv">
	  本文总阅读量<span id="busuanzi_value_page_pv"></span>次
	</span>

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <p>　　学习和使用ASP.NET MVC也有一段时间了，对这种开发方式(模式)有了更进一步的了解，今天就跳出ASP.NET的圈子，说一说我了解到的MVC，以及延伸到的MVP、MVVM。(如果理解的不到位希望有看到的这篇博文的同学在评论中指出😛)</p>
<h2 id="MVC"><a href="#MVC" class="headerlink" title="MVC"></a>MVC</h2><p>　　一开始接触MVC时最先想到的是，MVC算不算是一种设计模式？为什么GoF的23种设计模式中没有MVC模式？ <a id="more"></a><br>　　这个问题困扰了我很长时间，常常疑惑MVC为什么不是一种设计模式。之后看了大漠老师的一片博文<a href="http://damoqiongqiu.iteye.com/blog/1949256" target="_blank" rel="noopener">《为什么MVC不是一种设计模式？ —比较Backbone和Ext4.x在MVC实现上的差异》</a>，反复读了几遍，感觉获益颇深。<br>　　今天我们暂时不讨论这个问题，我的啰嗦就此作罢，进入正题。😛😛😛</p>
<p>　　按照惯例，上图！👀<br><img src="http://ww2.sinaimg.cn/mw690/c55a7aeejw1f1d510o2nfj20dt08ht8y.jpg" alt=""><br>　　如图中所示的，M、V、C三个组件都是单向联系的关系。<br>　　■ View(V):首先View在交互层面被触发，将信息传递给Controller(C)。<br>　　■ Controller(C):Controller收到了来自View的交互请求，判断该请求需要执行的操作和获取的数据。然后将View所需的数据信息发送给Model(M)。<br>　　■ Model(M):Model接收到了请求数据的消息，然后组织所需的数据，交给View展示。<br>　　上面所述的算是MVC的一个完整的执行流程，可能在不同的技术栈中实现是不相同的，但是大致上架构思路却也相同。</p>
<h2 id="MVP"><a href="#MVP" class="headerlink" title="MVP"></a>MVP</h2><p><img src="http://ww1.sinaimg.cn/mw690/c55a7aeejw1f17gi6rfa9j20e308yaac.jpg" alt=""><br>　　作为MVC的延伸，MVP取消掉了View(V)和Model(M)之间的联系，并且将单向的关系变成了双向。<br>　　■ 将Controller(C)变成了Presenter(P)，并且View(V)的变化由Presenter负责，因为它们之间是双向关系。<br>　　■ Presenter(P)负责了主要的业务逻辑，View和Model都由它来负责了。</p>
<h2 id="MVVM"><a href="#MVVM" class="headerlink" title="MVVM"></a>MVVM</h2><p><img src="http://ww2.sinaimg.cn/mw690/c55a7aeejw1f17gunrixcj20dp090t8z.jpg" alt=""><br>　　与MVP很相似，切断了Model(M)和View(V)之间的联系；比MVP更有趣的地方在于，ViewModel(VM)和View(V)之间是双向绑定的关系，也就是只要一方更新，另一方会自动的接受信息并相应的更新。如果学过WPF或者AngularJS这种体会会更深刻。<br>　　■ ViewModel和View之间双向绑定，一方的变动会自动的反应在另一方。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>　　① MVC模式中，有一部分的交互逻辑可能会出现在View层，造成View太厚，所以MVP的出现就是要将主要的代码逻辑放在Presenter中，让View只做展示。<br>　　② MVC模式有两种可能的交互方式：第一种，直接在View层交互，比如用户点击按钮等触发操作；第二种就是直接触发Controller，比如直接修改输入URL连接。<br>　　③ 在MVP和MVVM模式中，P或者VM包揽了大部分的交互逻辑，View只负责展示数据。<br>　　④ 三种模式的优点在于将关注点分离，每一个组件都只负责自己的事情，比如Controller、Persenter、ViewModel只负责业务逻辑的处理，它不用去管Model是怎么处理数据的、View是怎么展示的，甚至并不知道这两者的存在。<br>　　⑤ 在ASP.NET MVC中View负责了一部分的展示逻辑处理，使用<code>HtmlHelper</code>处理一些交互逻辑，但是在一个大项目中，可能对前端人员造成困扰，它们必须要能看懂这种特殊的语法。这种情况在MVC6中得到了改善，新的<code>HtmlHelper</code>语法更符合HTML标签的语义，让前端人员可以“猜”出来代码所表示的交互逻辑。</p>

      
    </div>
    <footer class="entry-meta entry-footer">
      
	<span class="ico-folder"></span>
    <a class="article-category-link" href="/categories/Asp-Net-MVC/">Asp.Net MVC</a>

      
  <span class="ico-tags"></span>
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Asp-Net-MVC/">Asp.Net MVC</a></li></ul>

      
        <div id="donation_div"></div>

<script src="/js/vdonate.js"></script>
<script>
var a = new Donate({
  title: '如果觉得我的文章对您有用，请随意打赏。您的支持将鼓励我继续创作!', // 可选参数，打赏标题
  btnText: 'Donate', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: '/css/images/wechatPay.png',
  alipayImage: '/css/images/alipay.jpg'
});
</script>
      

      
        
	<section id="comments" class="comment">
	  <div id="disqus_thread">
	  <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
	  </div>
	</section>

	<script type="text/javascript">
	var disqus_shortname = 'haijdblog';
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/count.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	</script>


      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2015/10/06/Of-the-nine-story-Monster-Tower/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          《九层妖塔》
        
      </div>
    </a>
  
  
    <a href="/2015/08/02/HTML-helper-methods/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">HTML辅助方法</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">Contents</strong>
    
      <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#MVC"><span class="nav-number">1.</span> <span class="nav-text">MVC</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#MVP"><span class="nav-number">2.</span> <span class="nav-text">MVP</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#MVVM"><span class="nav-number">3.</span> <span class="nav-text">MVVM</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#总结"><span class="nav-number">4.</span> <span class="nav-text">总结</span></a></li></ol>
    
    </div>
  </aside>
</section>
        
      </div>
      <footer id="footer" class="site-footer">
  

  <div class="clearfix container">
      <div class="site-info">
	      &copy; 2018 haijd All Rights Reserved.
          
            <span id="busuanzi_container_site_uv">
              本站访客数<span id="busuanzi_value_site_uv"></span>人次  
              本站总访问量<span id="busuanzi_value_site_pv"></span>次
            </span>
          
      </div>
      <div class="site-credit">
        Theme by <a href="https://github.com/iTimeTraveler/hexo-theme-hiero" target="_blank">hiero</a>
      </div>
  </div>
</footer>


<!-- min height -->

<script>
    var contentdiv = document.getElementById("content");

    contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("allheader").offsetHeight - document.getElementById("footer").offsetHeight + "px";
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>
<script src="/js/bootstrap.js"></script>
<script src="/js/main.js"></script>








	<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js" async=""></script>
</body>
</html>
